<template>
  <div class="all">
    <el-table :data="teacherInfo.arr" border v-loading="loading">
      <el-table-column fixed="left" prop="teacherId" label="教师编号" width="180"></el-table-column>
      <el-table-column prop="teacherName" label="姓名" width="180"></el-table-column>
      <el-table-column prop="institute" label="学院" width="200"></el-table-column>
      <el-table-column prop="sex" label="性别" width="120"></el-table-column>
      <el-table-column prop="tel" label="联系电话" width="120"></el-table-column>
      <el-table-column prop="email" label="邮箱地址" width="120"></el-table-column>
      <el-table-column prop="cardId" label="身份证号" width="120"></el-table-column>
      <el-table-column prop="type" label="职称" width="120"></el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template #default="scope">
          <el-button @click="editTea(scope.$index,scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="deleteTea(scope.row.userId)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-config-provider :locale="zhCn">
      <el-pagination
          @size-change="pageSizeChange"
          @current-change="pageNumChange"
          :current-page="pagination.pageNum"
          :page-sizes="[3, 6, 9, 12]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total">
      </el-pagination>
    </el-config-provider>
    <!-- 编辑对话框-->
    <el-dialog
        title="编辑教师信息"
        v-model="dialogVisible"
        width="30%"
        :before-close="handleClose">
      <section class="update">
        <el-form ref="changTeaRef" :model="teacherInfo.edit" label-width="80px">
          <el-form-item label="学号">
            <el-input v-model="teacherInfo.edit.teacherId" disabled></el-input>
          </el-form-item>
          <el-form-item label="姓名"
                        prop="teacherName"
                        :rules="[{required: true, message: '姓名不能为空', trigger: 'change'}]">
            <el-input v-model="teacherInfo.edit.teacherName"></el-input>
          </el-form-item>
          <el-form-item label="学院"
                        prop="institute"
                        :rules="[{required: true, message: '学院不能为空', trigger: 'change'}]">
            <el-input v-model="teacherInfo.edit.institute"></el-input>
          </el-form-item>
          <el-form-item label="性别"
                        prop="sex"
                        :rules="[
                            {required: true, message: '性别不能为空', trigger: 'change'},
                            {pattern: /^(男|女){1}$/, message: '只输入男/女'}
                      ]">
            <el-input v-model="teacherInfo.edit.sex"></el-input>
          </el-form-item>
          <el-form-item label="联系电话"
                        prop="tel"
                        :rules="[
                            {required: true, message: '联系电话不能为空', trigger: 'change'},
                            {pattern: /^(13[0-9]|14[1|4|5|6|7|8|9]|15[0|1|2|3|5|6|7|8|9]|17[2|3|5|6|7|8]|18[0-9]|19[1|8|9])\d{8}$/, message: '手机号格式不正确'}
                        ]">
            <el-input v-model="teacherInfo.edit.tel"></el-input>
          </el-form-item>
          <el-form-item label="邮箱地址"
                        prop="email"
                        :rules="[
                            {type: 'email', message: '请输入正确的邮箱', trigger: ['blur', 'change']},
                        ]">
            <el-input v-model="teacherInfo.edit.email"></el-input>
          </el-form-item>
          <el-form-item label="身份证号"
                        prop="cardId"
                        :rules="[{required: true, message: '身份证号不能为空', trigger: 'change'}]">
            <el-input v-model="teacherInfo.edit.cardId"></el-input>
          </el-form-item>
          <el-form-item label="职称">
            <el-input v-model="teacherInfo.edit.type"></el-input>
          </el-form-item>
        </el-form>
      </section>
      <template v-slot:footer>
        <el-button @click="editBack()">取 消</el-button>
        <el-button type="primary" @click="editSubmit()">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {getCurrentInstance, onMounted, reactive, ref} from "vue";
import {isObjectValueEqual} from "@/utils/funUtils";
import {ElMessage, ElMessageBox} from "element-plus";
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
export default {
  name: "tacherManage",
  setup(){
    const {proxy} = getCurrentInstance()
    let changTeaRef = ref(null)
    let loading = ref(false) //加载标识符
    let dialogVisible = ref(false)
    let pagination = reactive({
      pageNum: 1, //当前页
      total: Number(''), //记录条数
      pageSize: 3 //每页条数
    })
    let teacherInfo = reactive({
      arr: [],
      edit: {},
      editIndex: null,
      paper: []
    })
    let checkNumber = (rule,value,callback) => {
      const boolean = new RegExp('^[1-9][0-9]*$').test(value)
      if (!boolean) {
        callback(new Error('请输入正整数'))
      }else {
        callback()
      }
    }
    onMounted(()=>{
      getTeaInfo()
    })
    function getTeaInfo(){
      loading.value = true
      proxy.$axios.get(
          // eslint-disable-next-line no-undef
          `/manage/teachers/${pagination.pageNum}/${pagination.pageSize}`
      ).then(
          response => {
            teacherInfo.arr = [...response.data.list]
            pagination.total = response.data.total
            pagination.pageNum = response.data.pageNum
            pagination.pageSize = response.data.pageSize
            loading.value = false
          },
          error => {
            console.log('请求失败', error.message)
          }
      )
    }
    function pageSizeChange(value){//改变当前记录条数
      pagination.pageSize = value
      getTeaInfo()
    }
    function pageNumChange(value){//改变当前页码
      pagination.pageNum = value
      getTeaInfo()
    }
    function editTea(index,value){
      dialogVisible.value = true
      teacherInfo.editIndex = index
      teacherInfo.edit = {...value}
    }
    function editSubmit(){
      proxy.$refs.changTeaRef.validate((valid) => {
        if (valid) {
          if (isObjectValueEqual(teacherInfo.arr[teacherInfo.editIndex],teacherInfo.edit)){
            ElMessage({
              type: 'info',
              message: '信息没有改动',
            })
            return false
          }
          proxy.$axios.put(
              // eslint-disable-next-line no-undef
              `/manage/teacher/update`,
              {...teacherInfo.edit}
          ).then(
              response => {
                if (response.data) {
                  ElMessage({
                    type: 'success',
                    message: '修改成功',
                  })
                  dialogVisible.value = false
                  getTeaInfo()
                }
              },
              error => {
                console.log('请求失败', error.message)
              }
          )
        }else {
          return false
        }
      })
    }
    function editBack(){
      dialogVisible.value = false
      ElMessage({
        type: 'info',
        message: '取消修改',
      })
      proxy.$refs.changTeaRef.clearValidate()
    }
    function handleClose(done){
      ElMessageBox.confirm('确认关闭？',"",
          {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning',
          }
      ).then(() => {
        done();
        ElMessage({
          type: 'info',
          message: '取消修改',
        })
      }).catch(() => {});
    }
    function deleteTea(id) {
      ElMessageBox.confirm(
          '是否永久删除该学生信息,该操作不可逆！！！',
          '警告',
          {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning',
          }
      ).then(() => {
        proxy.$axios.delete(`/manage/teacher/delete/${id}`).then(
            response => {
              console.log(response.data)
              if (response.data){

                ElMessage({
                  type: 'success',
                  message: '删除成功',
                })
                getTeaInfo()
              }
            },
            error => {
              console.log('请求失败', error.message)
            }
        )
      }).catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })
    }
    return{
      changTeaRef,
      loading,
      dialogVisible,
      teacherInfo,
      pagination,
      checkNumber,
      pageNumChange,
      pageSizeChange,
      editTea,
      editBack,
      editSubmit,
      handleClose,
      deleteTea,
      zhCn
    }
  }
}
</script>

<style scoped>
.all {
  padding: 0px 40px;
}

.all .page {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.all .edit {
  margin-left: 20px;
}

.all .el-table tr {
  background-color: #dd5862 !important;
}

.el-table .warning-row {
  background: #000 !important;
}

.el-table .success-row {
  background: #dd5862;
}

</style>